package com.pxd.HandlerInterceptor;
//
/**
 * 进行拦截器的基本引用，对于方法重写
 */
//import io.micrometer.common.lang.Nullable;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//
//public class MyInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //请求参数之前
//        if (handler instanceof HandlerMethod) {
//            HandlerMethod method = (HandlerMethod) handler;
//            System.out.println(method.getMethod().getName());
//            Object value = request.getSession().getAttribute("value");
//            System.out.println(value);
//            return true;
//        }
//
////        return HandlerInterceptor.super.preHandle(request, response, handler);
//        return false;
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request
//            , HttpServletResponse response
//            , Object handler
//            , ModelAndView modelAndView) throws Exception {
////        请求参数中
//
//        System.out.println(request.getAttribute("value"));
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
////       请求参数之后，进行一系列参数的销毁
//        request.removeAttribute("value");
//        System.out.println("请求结束");
//        System.out.println(request.getAttribute("value"));
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }
//}
//@Configuration
//class interceptorconfig implements WebMvcConfigurer{
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        InterceptorRegistration regist=registry.addInterceptor(new MyInterceptor());
//        regist.addPathPatterns("/index");
//        WebMvcConfigurer.super.addInterceptors(registry);
//    }
//}
/**
 * 下半段用于拦截器的高频率访问
 */
//
//import jakarta.servlet.ServletContext;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//
//public class MyInterceptor implements HandlerInterceptor {
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        ServletContext context = request.getServletContext();
//        String sessionID = request.getSession().getId();
//        long now = System.currentTimeMillis();
//        Long Lasttime = (Long) context.getAttribute(sessionID);
//        if (Lasttime==null){
//            context.setAttribute(sessionID, now);
//            return true;
//        }else if (now-Lasttime>1000){
//            context.setAttribute(sessionID, now);
//            return true;
//        }else {
//            System.out.println("请求频率过高");
//            return false;
//        }
////
////
////        return HandlerInterceptor.super.preHandle(request, response, handler);
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }
//}
//@Configuration
//class InterceptorConfig implements WebMvcConfigurer{
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        InterceptorRegistration registration = registry.addInterceptor(new MyInterceptor());
//        registration.addPathPatterns("/**");
//        registration.excludePathPatterns("/aa");
//        WebMvcConfigurer.super.addInterceptors(registry);
//    }
//}
//@RestController
//class aa{
//    @RequestMapping("/aa")
//    public String aa() {
//        SimpleDateFormat sdf=new SimpleDateFormat("HH: mm:ss");
//        return "当前时间"+sdf.format(new Date());
//    }
//}

